package com.example.myapplication01.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication01.R;

import java.util.List;

/**
 * @user xunua
 * @email  xunua@outlook.com
 * @creat time 2020/4/10 17:13
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {
    private Context context;
    private List<String> list;
    private OnChildClickListener listener;
    private RecyclerView recyclerView;

    public void setOnChildClickListener(OnChildClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {//提供数据时候会使用到
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView=recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {//解绑
        super.onDetachedFromRecyclerView(recyclerView);
    }

    public MyAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//创建viewholder
        View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerview, parent,false);
        view.setOnClickListener(this);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {//把数据绑定在viewholder
        holder.textView.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //实现多布局服用


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onClick(View v) {
        if (recyclerView != null&listener!=null) {
            int position = recyclerView.getChildAdapterPosition(v);//获取这个v在adapter中的位置
            try {
                listener.onClick(recyclerView,v,position,list.get(position));
            }catch (Exception e){
                Log.e("TAG", "onClick: "+e );
            }
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.item_text);
        }
    }

    //监听事件
    public interface OnChildClickListener{
        void onClick(RecyclerView recyclerView, View view, int position, String data);
    }

    public void remove(int position){
        list.remove(position);
        notifyItemRemoved(position);
    }
    public void add(int position,String data){
        list.add(position,data);
        notifyItemInserted(position);
    }
    public void change(int position,String data){
        list.remove(position);
        list.add(position,data);
        notifyItemChanged(position);
    }
}
